package com.zros.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zros.modules.system.entity.Organization;
import com.zros.modules.system.entity.OrganizationUser;
import com.zros.modules.system.entity.Role;
import com.zros.modules.system.entity.UserRole;
import com.zros.modules.system.mapper.OrganizationUserMapper;
import com.zros.modules.system.service.IOrganizationService;
import com.zros.modules.system.service.IOrganizationUserService;
import com.zros.modules.system.service.IRoleService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jeebase
 * @since 2018-05-19
 */
@Service
public class OrganizationUserServiceImpl extends ServiceImpl<OrganizationUserMapper, OrganizationUser>
        implements IOrganizationUserService {
	
	@Autowired
    private IOrganizationService organizationService;
	
	@Override
	public List<Organization> queryOrgsByUserId(Long userId) {
        QueryWrapper<OrganizationUser> ew = new QueryWrapper<OrganizationUser>();
        ew.eq("user_id", userId);
        List<OrganizationUser> userRoleList = this.list(ew);
        if (!CollectionUtils.isEmpty(userRoleList)) {
            List<Long> orgIds = new ArrayList<Long>();
            for (OrganizationUser userRole : userRoleList) {
            	orgIds.add(userRole.getOrganizationId());
            }
            QueryWrapper<Organization> ewRole = new QueryWrapper<>();
            ewRole.in("id", orgIds);
            List<Organization> roleList = organizationService.list(ewRole);
            return roleList;
        } else {
            return null;
        }
    }
}
